﻿using OASystem.BLL.IService;
using OASystem.DAL;
using OASystem.Model;
using OASystem.ViewModel;
using System;
using System.Collections.Generic;
using System.Linq;

namespace OASystem.BLL.Service
{
    public class Document_wait : BaseRepository<Handle>, IDocument_wait
    {
        BaseRepository<Userinfo> uf = new BaseRepository<Userinfo>();
        /// <summary>
        /// 显示列表
        /// </summary>
        /// <param name="UserId">当前用户登录名</param>
        /// <param name="oneer">发布人</param>
        /// <param name="TaskTiltle">任务标题</param>
        /// <param name="CompletionTime">发布时间</param>
        /// <returns></returns>
        public ResponseResult Display(int UserId, string oneer, string TaskTiltle, string CompletionTime)
        {
            #region 实体对象
            ResponseResult result = new ResponseResult();
            Userinfo userinfo1 = new Userinfo();
            Userinfo userinfo2 = new Userinfo();
            #endregion
            #region 集合
            List<HandleQuery> taskQueries = new List<HandleQuery>();
            List<Handle> Task1 = new List<Handle>();
            #endregion
            #region 查询条件整理
            #region 防止异常处理
            if (oneer == "")
            { oneer = null; }
            if (TaskTiltle == "")
            { TaskTiltle = null; }
            if (CompletionTime == "")
            { CompletionTime = null; }
            #endregion
            //查询出自己的所有要办理的公文
            Task1 = QueryList(a => a.Oneer == UserId);
            //当发布人不为空时
            if (oneer != null)
            {
                //查出发布人所有信息，其实只要ID
                userinfo2 = uf.Query(a => a.EmployeeName.Contains(oneer));
                //防止查不出数据报错
                if (userinfo2 == null)
                { return null; }
                //在原来"查询出自己的任务"的基础上加上发布人ID
                Task1 = Task1.Where(a => a.Publisher == userinfo2.UserId).ToList();
            }
            //当任务标题不为空时
            if (TaskTiltle != null)
            {
                //如果上一个判断也不为空,就接在上一条查询记录的基础上加个条件
                Task1 = Task1.Where(a => a.TaskTiltle.Contains(TaskTiltle)).ToList();
            }
            //当发布时间不为空时
            if (CompletionTime != null)
            {
                //CompletionTime中的内容转为DateTime类型
                DateTime A1 = Convert.ToDateTime(CompletionTime);
                //同上,加个发布时间的条件
                Task1 = Task1.Where(a => a.CompletionTime <= A1).ToList();
            }
            #endregion
            #region 返回处理
            for (int i = 0; i < Task1.Count; i++)
            {
                //这个实体必须在循环内部，否则存入的数据将被最后一条记录所覆盖
                HandleQuery taskQuery = new HandleQuery();
                //这里要把int?转成int不然会报错
                int A2;
                A2 = Convert.ToInt32(Task1[i].Publisher);
                //查出发布人的所有信息
                userinfo1 = uf.Query(a => a.UserId == A2);
                //同上
                A2 = Convert.ToInt32(Task1[i].Oneer);
                //查出执行人的所有信息
                userinfo2 = uf.Query(a => a.UserId == A2);
                //当这俩查出来都不为null时
                if (userinfo1 != null && userinfo2 != null)
                {
                    #region 赋值
                    //拉出他的用户姓名给taskQuery对象赋值
                    taskQuery.Publisher = userinfo1.EmployeeName;
                    taskQuery.Oneer = userinfo2.EmployeeName;
                    taskQuery.Status = Task1[i].Status;
                    taskQuery.Id = Task1[i].Id;
                    taskQuery.TaskTiltle = Task1[i].TaskTiltle;
                    taskQuery.TaskContent = Task1[i].TaskContent;
                    taskQuery.CompletionTime = Task1[i].CompletionTime;
                    #endregion
                    //添加到集合
                    taskQueries.Add(taskQuery);
                }
                else
                {
                    return null;
                }
            }
            //添加到result对象中
            result.data = taskQueries;
            return result;
            #endregion
        }

        /// <summary>
        /// 完成状态
        /// </summary>
        /// <param name="id">所选行ID</param>
        /// <param name="status">修改后的状态</param>
        /// <returns></returns>
        public ResponseResult Updater(int id, int status)
        {
            ResponseResult result = new ResponseResult();
            Handle tl = new Handle();
            tl = Query(a => a.Id == id);
            tl.Status = status;
            result.success = Update(tl);
            return result;
        }
    }
}
